package com.jghz.autoconfigure.hibernate;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.type.Type;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.hibernate.UnByteCodeProxyInterceptor;

public class CustomEmptyInterceptor extends UnByteCodeProxyInterceptor {

	private static final long serialVersionUID = -3622415500115328035L;

	private String createEmpProperty = "createEmp";
	private String createDateProperty = "createDate";
	private String updateEmpProperty = "updateEmp";
	private String updateDateProperty = "updateDate";

	private Set<String> createEmpPropertyClass = new HashSet<>();
	private Set<String> createDatePropertyClass = new HashSet<>();
	private Set<String> updateEmpPropertyClass = new HashSet<>();
	private Set<String> updateDatePropertyClass = new HashSet<>();

	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
		boolean ischanged = false;
		for (int i = 0; i < propertyNames.length; i++) {
			if (isCreateEmpProperty(propertyNames[i])) {
				state[i] = ContextHolder.getLoginUserName();
				ischanged = true;
			}
			if (isCreateDateProperty(propertyNames[i])) {
				state[i] = new Date();
				ischanged = true;
			}
		}
		return ischanged;
	}

	@Override
	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
			String[] propertyNames, Type[] types) {
		boolean ischanged = false;
		for (int i = 0; i < propertyNames.length; i++) {
			if (isUpdateDateProperty(propertyNames[i])) {
				currentState[i] = new Date();
				ischanged = true;
			}
			if (isUpdateEmpProperty(propertyNames[i])) {
				currentState[i] = ContextHolder.getLoginUserName();
				ischanged = true;
			}
		}
		return ischanged;
	}

	public String getCreateEmpProperty() {
		return createEmpProperty;
	}

	// @Value("${entity.createEmpProperty}")
	public void setCreateEmpProperty(String createEmpProperty) {
		if (!createEmpProperty.isEmpty()) {
			this.createEmpProperty = createEmpProperty;
		}

		String[] classNames = this.createEmpProperty.split(",");
		for (String name : classNames) {
			this.createEmpPropertyClass.add(name.trim());
		}
	}

	public String getCreateDateProperty() {
		return createDateProperty;
	}

	// @Value("${entity.createDateProperty}")
	public void setCreateDateProperty(String createDateProperty) {
		if (!createDateProperty.isEmpty()) {
			this.createDateProperty = createDateProperty;
		}

		String[] classNames = this.createDateProperty.split(",");
		for (String name : classNames) {
			this.createDatePropertyClass.add(name.trim());
		}
	}

	public String getUpdateEmpProperty() {
		return updateEmpProperty;
	}

	// @Value("${entity.updateEmpProperty}")
	public void setUpdateEmpProperty(String updateEmpProperty) {
		if (!updateEmpProperty.isEmpty()) {
			this.updateEmpProperty = updateEmpProperty;
		}

		String[] classNames = this.updateEmpProperty.split(",");
		for (String name : classNames) {
			this.updateEmpPropertyClass.add(name.trim());
		}
	}

	public String getUpdateDateProperty() {
		return updateDateProperty;
	}

	// @Value("${entity.updateDateProperty}")
	public void setUpdateDateProperty(String updateDateProperty) {
		if (!updateDateProperty.isEmpty()) {
			this.updateDateProperty = updateDateProperty;
		}

		String[] classNames = this.updateDateProperty.split(",");
		for (String name : classNames) {
			this.updateDatePropertyClass.add(name.trim());
		}
	}

	protected boolean isCreateEmpProperty(String str) {
		return this.createEmpPropertyClass.contains(str);
	}

	protected boolean isCreateDateProperty(String str) {
		return this.createDatePropertyClass.contains(str);
	}

	protected boolean isUpdateEmpProperty(String str) {
		return this.updateEmpPropertyClass.contains(str);
	}

	protected boolean isUpdateDateProperty(String str) {
		return this.updateDatePropertyClass.contains(str);
	}
}
